首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
缺失的第一个正整数
[编程题]缺失的第一个正整数
热度指数:76807
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
给定一个无重复元素的整数数组nums,请你找出其中没有出现的最小的正整数
进阶
: 空间复杂度
,时间复杂度
数据范围:
示例1
输入
[1,0,2]
输出
3
示例2
输入
[-2,3,4,1,5]
输出
2
示例3
输入
[4,5,6,8,9]
输出
1
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(633)
分享
提交结果有问题?
3个回答
176篇题解
添加回答
0
JavaScript Node
萌萌94
/**
方法三:
将有效数字(1 <= val <= N)移动到原本应该所在的位置 [0..N-1]
**/
function minNumberDisappeared( nums ) {
// write code here
let temp, N = nums.length, val;
const swap = (i, j) => {
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
for (let i = 0; i < N; i++) {
while (nums[i] > 0 && nums[i] <= N && nums[i] != i+1 && nums[i] != nums[nums[i]-1]) {
swap(i, nums[i]-1);
}
}
for (let i = 0; i < N; i++) {
if (nums[i]!=i+1) {
return i+1;
}
}
return N+1;
}
发表于 2022-05-26 18:17:57
回复(0)
2
JavaScript Node
summer_swilder
function minNumberDisappeared( nums ) {
let obj = {};
for (let i = 0; i<nums.length; i++) {
obj[nums[i]] = 1;
}
let i = 1;
while (obj[i]) {
i++;
}
retu
rn i;
}
module.exports = {
minNumberDisappeared : minNumberDisappeared
};
发表于 2022-04-22 22:56:37
回复(0)
这道题你会答吗?花几分钟告诉大家答案吧!
提交观点
问题信息
数组
哈希
上传者:
牛客301499号
难度:
3条回答
633收藏
4265浏览
热门推荐
通过挑战的用户
查看代码
牛客89418...
2022-10-27 16:32:27
牛客86531...
2022-09-16 18:53:41
Daoremi
2022-09-16 17:29:06
粗茶
2022-09-16 17:03:01
牛客13682...
2022-09-16 16:52:44
相关试题
设某二叉树的先序遍历序列为abdg...
树
评论
(1)
游戏内数据分析涉猎的少,如何证明自...
评论
(1)
之前的经历中单品数据分析的经验丰富...
评论
(1)
什么样的人适合做数据分析
评论
(1)
2022 诺瓦科技 Perl re...
perl
System Verilog
评论
(1)
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型 */ public int minNumberDisappeared (int[] nums) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @return int整型 */ int minNumberDisappeared(vector
& nums) { // write code here } };
#coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @return int整型 # class Solution: def minNumberDisappeared(self , nums ): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型 */ public int minNumberDisappeared (List
nums) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型 */ function minNumberDisappeared( nums ) { // write code here } module.exports = { minNumberDisappeared : minNumberDisappeared };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @return int整型 # class Solution: def minNumberDisappeared(self , nums: List[int]) -> int: # write code here
package main import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型 */ func minNumberDisappeared( nums []int ) int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param numsLen int nums数组长度 * @return int整型 */ int minNumberDisappeared(int* nums, int numsLen ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @return int整型 # class Solution def minNumberDisappeared(nums) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型 */ def minNumberDisappeared(nums: Array[Int]): Int = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型 */ fun minNumberDisappeared(nums: IntArray): Int { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型 */ public int minNumberDisappeared (int[] nums) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型 */ export function minNumberDisappeared(nums: number[]): number { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型 */ func minNumberDisappeared ( _ nums: [Int]) -> Int { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型 */ pub fn minNumberDisappeared(&self, nums: Vec
) -> i32 { // write code here } }
[1,0,2]
3
[-2,3,4,1,5]
2
[4,5,6,8,9]
1